//
//  MyMoneyViewController.m
//  HTP
//
//  Created by 王鑫 on 15/11/1.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "MyMoneyViewController.h"
#import "MyMoneyTableViewCell.h"

//Model
#import "MyMoneyViewModel.h"
#import "ProfileModel.h"

//ViewController
#import "MyCouponsViewController.h"
#import "MyBillsViewController.h"
#import "ProfileOneViewController.h"

#define headfootbutton ScreenWidth / 3.0

@interface MyMoneyViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic,strong) UITableView *myMoneyTable;

@property (nonatomic,retain) NSMutableArray *cellDate;

@property (nonatomic,retain) NSDictionary *myBills;

@property (nonatomic,strong) MyMoneyModel *basicInfoM;

//TODO: 界面信息
//余额
@property (nonatomic,retain) UILabel *moneyNumberLabel;
//卡卷数量
@property (nonatomic,retain) UILabel *couponsNumberLabel;
//课程数
@property (nonatomic,retain) UILabel *lessonNumberLabel;
//昵称
@property (nonatomic,retain) UILabel *infoNameLabel;
//电话号码
@property (nonatomic,retain) UILabel *infoPhoneLabel;
//头像
@property (nonatomic,retain) UIImageView *infoImage;

@end

@implementation MyMoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getBasicInfoData];
    [self initNav];
    [self setViewData];
    
}


//TODO:导航栏定义
- (void)initNav{
    
    [self addCustomBackBase];
    [self addCustomTitleWithTitle:@"我的钱包"];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc]init]];
    
}

//TODO:UI绘制
- (void)initUI {
    
    [self setData];
    [self.view addSubview:[self getMyMoneyHeadView]];
    [self.view addSubview:self.myMoneyTable];
}

//TODO:上面部分UI绘制
- (UIView *)getMyMoneyHeadView {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 170)];
    
    UIButton *personalInfoButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 85)];
    [personalInfoButton setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"infor"]]];
    [personalInfoButton addTarget:self action:@selector(goToProfile) forControlEvents:UIControlEventTouchUpInside];
    
    _infoImage = [[UIImageView alloc] initWithFrame:CGRectMake((headfootbutton - 60) / 2.0, 5, 65, 65)];
    _infoImage.layer.cornerRadius = 32.5;
    _infoImage.layer.masksToBounds = YES;
//    _infoImage.image = [UIImage imageNamed:@"我的钱包_03"];
    [personalInfoButton addSubview:_infoImage];
    
    _infoNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(105, 10, ScreenWidth - 150, 25)];
    _infoNameLabel.font = [UIFont systemFontOfSize:25.0f];
    _infoNameLabel.text = @"青苗家教";
    _infoNameLabel.textColor = [UIColor whiteColor];
    [personalInfoButton addSubview:_infoNameLabel];
    
    _infoPhoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(105, 40, 110, 25)];
    _infoPhoneLabel.font = [UIFont systemFontOfSize:16.0f];
    _infoPhoneLabel.text = @"";
    _infoPhoneLabel.textColor = [UIColor whiteColor];
    [personalInfoButton addSubview:_infoPhoneLabel];
    
    UIImageView *inforNextImage = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth - 30, 24, 15, 26.5)];
    inforNextImage.image = [UIImage imageNamed:@"我的钱包_06"];
    [personalInfoButton addSubview:inforNextImage];
    
    [headView addSubview:personalInfoButton];
    
    UIButton *moneyButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 85, headfootbutton, 85)];
    _moneyNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, headfootbutton, 40)];
    _moneyNumberLabel.textAlignment = _TEXT_ALIGN_CENTER;
    _moneyNumberLabel.textColor = [UIColor colorWithRed:235 / 255.0 green:161 / 255.0 blue:0 alpha:1];
    _moneyNumberLabel.font = [UIFont systemFontOfSize:12.0f];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"0.00元"]];
    NSUInteger length = [NSString stringWithFormat:@"0.00元"].length;
    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, length - 1)];
    _moneyNumberLabel.attributedText = str;
    [moneyButton addSubview:self.moneyNumberLabel];
    
    
    UILabel *moneyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 55, headfootbutton, 20)];
    moneyLabel.textAlignment = _TEXT_ALIGN_CENTER;
    moneyLabel.font = [UIFont systemFontOfSize:13.0f];
    moneyLabel.textColor = RGBCOLOR(171, 171, 171, 1);
    moneyLabel.text = @"我的余额";
    [moneyButton addSubview:moneyLabel];
    
    [headView addSubview:moneyButton];
    [headView addSubview:[self buttonLine:CGRectMake(headfootbutton - 0.5, 85, 1, 85)]];
    
    UIButton *couponsButton = [[UIButton alloc] initWithFrame:CGRectMake(headfootbutton, 85, headfootbutton, 85)];
    [couponsButton addTarget:self action:@selector(goToCouponsView) forControlEvents:UIControlEventTouchUpInside];
    _couponsNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, headfootbutton, 40)];
    _couponsNumberLabel.textAlignment = _TEXT_ALIGN_CENTER;
    _couponsNumberLabel.textColor = [UIColor redColor];
    _couponsNumberLabel.font = [UIFont systemFontOfSize:12.0f];
    str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"0个"]];
    length = [NSString stringWithFormat:@"0个"].length;

    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, length - 1)];
    _couponsNumberLabel.attributedText = str;
    [couponsButton addSubview:self.couponsNumberLabel];
    
    UILabel *couponsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 55, headfootbutton, 20)];
    couponsLabel.textAlignment = _TEXT_ALIGN_CENTER;
    couponsLabel.font = [UIFont systemFontOfSize:13.0f];
    couponsLabel.textColor = RGBCOLOR(171, 171, 171, 1);
    couponsLabel.text = @"优惠劵";
    [couponsButton addSubview:couponsLabel];
    
    [headView addSubview:couponsButton];
    [headView addSubview:[self buttonLine:CGRectMake(headfootbutton * 2 - 0.5, 85, 1, 85)]];
    
    UIButton *lessonButton = [[UIButton alloc] initWithFrame:CGRectMake(headfootbutton * 2, 85, headfootbutton, 85)];
    _lessonNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, headfootbutton, 40)];
    _lessonNumberLabel.textAlignment = _TEXT_ALIGN_CENTER;
    _lessonNumberLabel.textColor = [UIColor colorWithRed:148 / 255.0 green:191 / 255.0 blue:109 / 255.0 alpha:1];
    _lessonNumberLabel.font = [UIFont systemFontOfSize:12.0f];
    str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"0个"]];
    length = [NSString stringWithFormat:@"0个"].length;

    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, length - 1)];
    _lessonNumberLabel.attributedText = str;
    [lessonButton addSubview:self.lessonNumberLabel];
    
    UILabel *lessonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 55, headfootbutton, 20)];
    lessonLabel.textAlignment = _TEXT_ALIGN_CENTER;
    lessonLabel.font = [UIFont systemFontOfSize:13.0f];
    lessonLabel.textColor = RGBCOLOR(171, 171, 171, 1);
    lessonLabel.text = @"课程";
    [lessonButton addSubview:lessonLabel];
    
    [headView addSubview:lessonButton];
    
    headView.backgroundColor = [UIColor whiteColor];
    return headView;
}

- (UIImageView *)buttonLine:(CGRect)rect {
    UIImageView *line = [[UIImageView alloc] initWithFrame:rect];
    line.image = [UIImage imageNamed:@"line_qianbao"];
    return line;
}

//TODO:重写get
- (UITableView *)myMoneyTable {
    if (!_myMoneyTable) {
        _myMoneyTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 170, ScreenWidth, ScreenHeihgt - 64) style:UITableViewStyleGrouped];
        [_myMoneyTable setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_qianbao"]]];
        _myMoneyTable.delegate = self;
        _myMoneyTable.dataSource = self;
        _myMoneyTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    
    return _myMoneyTable;
}

//TODO:tableview的代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else
        return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *Identifier = @"Identifier";
    MyMoneyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
    
    if (cell == nil) {
        cell = [[MyMoneyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    }
    
    [cell setCellData:[_cellDate objectAtIndex:indexPath.section]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        MyBillsViewController *myBillsView = [[MyBillsViewController alloc] init];
        [myBillsView addCustomTitleWithTitle:@"我的账单"];
        [[NSNotificationCenter defaultCenter] postNotificationName:ROOTGOTONEXTCONTROLLER object:myBillsView];
    }
}

//TODO: 获得数据
- (void)getBasicInfoData {
    MyMoneyViewModel *myMoneyVM = [[MyMoneyViewModel alloc] init];
    [myMoneyVM setBlockWithReturnBlock:^(id returnValue) {
        [self hiddenMbProgress];
        [self dictionaryToModel:(NSDictionary *)returnValue];
        [self setBasicData];
    }WithErrorBlock:^(id errorCode){
        [self hiddenMbProgress];
    }WithFailureBlock:^{
        [self hiddenMbProgress];
    }];
    [self showMbProgress:@""];
    [myMoneyVM getBasicInfoByParentId:[USERCONTENT getUserParentId]];
}

//TODO: 数据处理

//字典 -> 模型
- (void)dictionaryToModel:(NSDictionary *)message {
    DLog(@"%@",message[INTERFACE_INFO]);
    _basicInfoM = [MyMoneyModel mj_objectWithKeyValues:message[INTERFACE_INFO]];
}

/*数据设置*/
//余额，卡卷，课程的数据设置
- (void)setBasicData {
    //余额设置
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f元",[_basicInfoM.mwValue floatValue]]];
    NSUInteger length = [NSString stringWithFormat:@"%.2f元",[_basicInfoM.mwValue floatValue]].length;
    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, length - 1)];
    _moneyNumberLabel.attributedText = str;
    
    //卡卷数量设置
    str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%d个",[_basicInfoM.couponNumber intValue]]];
    length = [NSString stringWithFormat:@"%d个",[_basicInfoM.couponNumber intValue]].length;
    
    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, length - 1)];
    _couponsNumberLabel.attributedText = str;
    
    //课程数量设置
    str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%d个",[_basicInfoM.courseNumber intValue]]];
    length = [NSString stringWithFormat:@"%d个",[_basicInfoM.courseNumber intValue]].length;
    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, length - 1)];
    _lessonNumberLabel.attributedText = str;
}

//选项的设置
- (void)setData {
    _myBills = @{
                 @"icoName":@"zhangdan",
                 @"infoText":@"我的账单",
                 };
    _cellDate = [NSMutableArray arrayWithObjects: _myBills, nil];
}

//个人信息数据设置
-(void)setViewData{
    ProfileModel *profile = [ProfileModel mj_objectWithKeyValues:[USERCONTENT getDefaultUser]];
    _infoNameLabel.text = profile.prChildName;
    _infoPhoneLabel.text = profile.userName;
    [_infoImage setNormalImageUrl:profile.userPhoto anplaceholder:@"m_user_y"];
}


//TODO: Action

// 跳转到卡卷列表
- (void)goToCouponsView {
    MyCouponsViewController *couponsView = [[MyCouponsViewController alloc] init];
    [self.navigationController pushViewController:couponsView animated:YES];
}

//跳转到个人信息
- (void)goToProfile {
    ProfileOneViewController *profileVC = [[ProfileOneViewController alloc] init];
    [[NSNotificationCenter defaultCenter] postNotificationName:ROOTGOTONEXTCONTROLLER object:profileVC];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
